#!/bin/bash

# This function is passed the build instruction for a single job.
# The function will run either "build <package>" or "install <package>".
# ${slot} is the job slot number, ie. 1-8 when THREADCOUNT=8.
# ${job} is the sequence within the total number of ${jobs}.
package_worker() {
  local slot=$1 job=$2 jobs=$3 maxslot=$4 task="$5" pkgname="$6" oseqinfo="$7"
  local result status
  local addon istarget isaddon

  export MTJOBID=${slot} PARALLEL_SEQ=${job} MTMAXJOBS=${jobs} MTMAXSLOT=${maxslot}

  . config/options "${pkgname}"

  if [ -z "${oseqinfo}" ]; then
    ${SCRIPTS}/${task} ${pkgname} 2>&1 && result=0 || result=1
  else
    print_color CLR_ERROR "FAILURE [${task} ${pkgname}]: a previous dependency process has already failed!"
    echo
    echo

    num=0
    for failed_items in ${oseqinfo//;/ }; do
      num=$((num + 1))
      read -r ftask fpkgname fseq <<< "${failed_items//,/ }"

      if [ -n "${fseq}" ]; then
        [ ${num} -eq 1 ] && echo "The following log(s) for already failed dependencies are available:"
        printf "  %-7s %s => %s\n" "${ftask}" "${fpkgname}" "${THREAD_CONTROL}/logs/${fseq}.log"
      else
        print_color CLR_ERROR "ALREADY FAILED [${ftask} ${fpkg}]"
        echo
      fi
    done
    echo
    result=1
  fi

  [[ ${pkgname} =~ :target$ || "${pkgname//:/}" = "${pkgname}" ]] && istarget="yes" || istarget="no"

  [[ "${MTADDONBUILD}" = "yes" && ( "${PKG_IS_ADDON}" = "yes" || "${PKG_IS_ADDON}" = "embedded" ) ]] && isaddon="yes" || isaddon="no"

  if [ "${isaddon}" = "yes" -a "${istarget}" = "yes" ]; then
    if [ ${result} -eq 0 ]; then
      ${SCRIPTS}/install_addon ${pkgname} 2>&1 && result=0 || result=1
    fi

    if [ ${result} -ne 0 ]; then
      if [ -d "${THREAD_CONTROL}/logs" ]; then
        echo "${PKG_NAME} ${THREAD_CONTROL}/logs/${job}.log" >>"${THREAD_CONTROL}/addons.failed"
      else
        echo "${PKG_NAME}" >>"${THREAD_CONTROL}/addons.failed"
      fi
    fi
  fi

  (
    flock --exclusive 95
    [ ${result} -eq 0 ] && status="DONE" || status="FAIL"
    num=$(< "${THREAD_CONTROL}/progress")
    mv "${THREAD_CONTROL}/progress" "${THREAD_CONTROL}/progress.prev"
    num=$((num + 1))
    echo ${num} >"${THREAD_CONTROL}/progress"
  ) 95>"${THREAD_CONTROL}/locks/.progress"

  if [ ${result} -eq 0 ]; then
    pkg_lock_status "IDLE"
  else
    pkg_lock_status "FAILED" "${pkgname}" "${task}"

    print_color CLR_ERROR "FAILURE: $SCRIPTS/${task} ${pkgname} has failed!"
    echo
  fi

  return ${result}
}

package_worker "$1" "$2" "$3" "$4" "$5" "$6" "$7"
